package com.hskn.hss.utils;

import cn.hutool.core.bean.BeanUtil;
import com.hskn.hss.entity.MenuTreeEntity;
import com.hskn.hss.module.button.entity.SysButton;
import com.hskn.hss.module.menu.entity.SysMenu;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

public class MenuTreeUtil {
    // RecursiveEquipmentType

    /**
     * 返回所有的子子孙孙树级结构
     *
     * @param treeNode
     * @param menuTreeEntityMap parentId分组
     * @return
     */
    private static List<MenuTreeEntity> getAllChildTreeList(MenuTreeEntity treeNode, Map<String, List<MenuTreeEntity>> menuTreeEntityMap) {
        Map<String, List<MenuTreeEntity>> menuTreeEntityMap1 = new HashMap<>();
        BeanUtil.copyProperties(menuTreeEntityMap, menuTreeEntityMap1);
        if (Objects.nonNull(treeNode) && menuTreeEntityMap1.containsKey(treeNode.getId())) {
            List<MenuTreeEntity> menuTreeEntities = menuTreeEntityMap1.get(treeNode.getId());
            for (MenuTreeEntity menuTreeEntity : menuTreeEntities) {
                if (menuTreeEntityMap1.containsKey(menuTreeEntity.getParentid())) {
                    List<MenuTreeEntity> m = getAllChildTreeList(menuTreeEntity, menuTreeEntityMap1);
                    if (CollectionUtils.isNotEmpty(m)) {
                        m = m.stream().sorted(Comparator.comparing(MenuTreeEntity::getName)).filter(menuTreeEntity1 -> menuTreeEntity1.getParentid().equals(treeNode.getId())).collect(Collectors.toList());
                    }
                    if (CollectionUtils.isEmpty(menuTreeEntity.getChildren())) {
                        menuTreeEntity.setChildren(m);
                    }
                }
            }
            return menuTreeEntityMap1.get(treeNode.getId());
        } else {
            return null;
        }
    }

    /**
     * 返回所有的子子孙孙
     *
     * @param sysMenu
     * @param sysMenuMap id分组
     * @return
     */
    public static List<SysMenu> getAllChildList(SysMenu sysMenu, Map<String, SysMenu> sysMenuMap) {
        List<SysMenu> resultList = new ArrayList<>();
        resultList.add(sysMenu);
        if (sysMenuMap.containsKey(sysMenu.getParentid()) && Objects.nonNull(sysMenuMap.get(sysMenu.getParentid()))) {
            SysMenu parentMenu = sysMenuMap.get(sysMenu.getParentid());
            resultList.addAll(getAllChildList(parentMenu, sysMenuMap));
            return resultList;
        }
        return resultList.stream().distinct().collect(Collectors.toList());

    }

    /**
     * 构造菜单按钮树
     *
     * @param treeNodeList
     * @param menuTreeEntityMap
     * @param sysButtonMap
     * @return
     */
    public static List<MenuTreeEntity> buildTreeList(List<MenuTreeEntity> treeNodeList, Map<String, List<MenuTreeEntity>> menuTreeEntityMap
            , Map<String, List<SysButton>> sysButtonMap) {
        for (MenuTreeEntity menuTreeEntity : treeNodeList) {
            Map<String, List<MenuTreeEntity>> menuTreeEntityMap1 = new HashMap<>();
            BeanUtil.copyProperties(menuTreeEntityMap, menuTreeEntityMap1);
            menuTreeEntity.setChildren(getAllChildTreeList(menuTreeEntity, menuTreeEntityMap1));
        }
        for (MenuTreeEntity menuTreeEntity : treeNodeList) {
            if (sysButtonMap.containsKey(menuTreeEntity.getId()) && CollectionUtils.isEmpty(menuTreeEntity.getChildren())) {
                List<MenuTreeEntity> menuTreeEntityList = MenuTreeEntity.buildMenuTreeEntityList(sysButtonMap.get(menuTreeEntity.getId()))
                        .stream().sorted(Comparator.comparing(MenuTreeEntity::getName)).collect(Collectors.toList());
                menuTreeEntity.setChildren(menuTreeEntityList);
            }
        }
        return treeNodeList.stream().filter(menuTreeEntity -> "0".equals(menuTreeEntity.getParentid())).collect(Collectors.toList());
    }
}
